﻿@using System.Text;
@typeparam TValue
@inherits Blazorise.Radio<TValue>
@if ( AsButton )
{
    <label for="@ElementId" class="@ButtonLabelClassNames">
        @ChildContent
        <input @ref="@ElementRef" id="@ElementId" type="radio" class="@ClassNames" style="@ButtonInputStyleNames" disabled="@IsDisabled" readonly="@ReadOnly" checked="@Checked" value="@Value" tabindex="@TabIndex" @onchange="@OnChangeHandler" @onkeydown="@OnKeyDownHandler" @onkeypress="@OnKeyPressHandler" @onkeyup="@OnKeyUpHandler" @onblur="@OnBlurHandler" @onfocus="@OnFocusHandler" @onfocusin="@OnFocusInHandler" @onfocusout="@OnFocusOutHandler" name="@Group" @attributes="@Attributes" />
    </label>
    @Feedback
}
else
{
    <Control Role="ControlRole.Radio" Inline="@(ParentRadioGroup?.Inline ?? Inline)">
        @RadioFragment
    </Control>
}
@code {
    string ButtonLabelClassNames
    {
        get
        {
            var sb = new StringBuilder( "relative focus:ring-4 font-medium focus:outline-none" );

            sb.Append( ' ' ).Append( ClassProvider.ButtonSize( ThemeSize, false ) );

            if ( ParentRadioGroup?.Orientation == Orientation.Horizontal )
                sb.Append( " rounded-none first:rounded-l-lg last:rounded-r-lg" );
            else
                sb.Append( " rounded-none first:rounded-t-lg last:rounded-b-lg w-full" );

            if ( Checked )
                sb.Append( " text-white bg-secondary-700 hover:bg-secondary-800 focus:ring-secondary-300 dark:bg-secondary-600 dark:hover:bg-secondary-700 dark:focus:ring-secondary-800" );
            else
                sb.Append( " text-white bg-secondary-500 hover:bg-secondary-600 focus:ring-secondary-100 dark:bg-secondary-400 dark:hover:bg-secondary-500 dark:focus:ring-secondary-600" );

            if ( Disabled )
                sb.Append( " cursor-not-allowed opacity-60" );

            return sb.ToString();
        }
    }

    string ButtonInputStyleNames
    {
        get
        {
            var sb = new StringBuilder( "clip: rect(0,0,0,0);" );

            if ( StyleNames is not null )
                sb.Append( StyleNames );

            return sb.ToString();
        }
    }

    protected RenderFragment RadioFragment => __builder =>
    {
        <input @ref="@ElementRef" id="@ElementId" type="radio" class="@ClassNames" style="@StyleNames" disabled="@IsDisabled" readonly="@ReadOnly" checked="@Checked" value="@Value" tabindex="@TabIndex" @onchange="@OnChangeHandler" @onkeydown="@OnKeyDownHandler" @onkeypress="@OnKeyPressHandler" @onkeyup="@OnKeyUpHandler" @onblur="@OnBlurHandler" @onfocus="@OnFocusHandler" @onfocusin="@OnFocusInHandler" @onfocusout="@OnFocusOutHandler" name="@Group" @attributes="@Attributes" />
        <Label Type="LabelType.Radio" For="@ElementId" Style="@Style" Cursor="@Cursor">@ChildContent</Label>
        @Feedback
    };
}